package leetcode.code2075;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public String decodeCiphertext(String encodedText, int rows) {
		char[] cs = encodedText.toCharArray();
		// 列数
		int len = cs.length / rows;
		StringBuffer ans = new StringBuffer();
		int size = 0;
		// 第一行每一列作为开始
		for (int i = 0; i < len; i++) {
			// 字符起点
			int index = i;
			for (int j = 0; j < rows && index < cs.length; j++) {
				ans.append(cs[index]);
				// 记录尾部连续的空格
				size = cs[index] == ' ' ? size + 1 : 0;
				// 下一个字符的位置
				index += len + 1;
			}
		}
		return ans.substring(0, ans.length() - size);
	}

	@Override
	public void debug4() {
		H.compare("i love leet  de", this.decodeCiphertext("ive     eed   l te   ol ", 4));
		H.compare("i love leetcode", this.decodeCiphertext("iveo    eed   l te   olc", 4));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
	}

}
